package com.xuecheng.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Dictionary;
import com.xuecheng.commons.model.vo.DictionaryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.basic.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 数据字典前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class DictionaryController {

    @Autowired
    private DictionaryService  dictionaryService;

    //查询所有数据字典
    @GetMapping("/dictionary")
    public ResponseResult findAll(){
        //1.调用service查询所有
        List<Dictionary> list = dictionaryService.list();
        //2.转换为Vo
        List<DictionaryVo> voList = BeanHelper.copyWithCollection(list, DictionaryVo.class);
        //3.返回结果
        return ResponseResult.okResult(voList);
    }
    //根据code查询数据字典
    @GetMapping("/dictionary/{code}")
    public ResponseResult findByCode(@PathVariable String code){
        //1.拼接条件
        LambdaQueryWrapper<Dictionary> qw = new LambdaQueryWrapper<>();
        qw.eq(Dictionary::getCode,code);
        //2.查询
        Dictionary dictionary = dictionaryService.getOne(qw);
        //3.转Vo
        DictionaryVo vo = BeanHelper.copyProperties(dictionary, DictionaryVo.class);
        //4.返回结果
        return ResponseResult.okResult(vo);

    }

}
